import React, { useState, createContext, useContext } from 'react';

const CountContext = createContext()

// 父组件
function Context() {
  const [count, setCount] = useState(0)

  return (  
    <>
      <p>你点击了 {count} 次</p>
      <button onClick={() => setCount(count + 1)}>点击</button>

      {/* 传值 */}
      <CountContext.Provider value={count}>
        {/* 接受 */}
        <Countr />
      </CountContext.Provider>
    </>
  )
}

// 子组件
function Countr() {
  const count = useContext(CountContext)
  return <h2>接收到了值 {count}</h2>
}

export default Context
